package com.opg.uop.manage.base;

import java.util.Collections;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 统一异常处理器
 * 
 * @author Yang Jiazhi
 * @date 2017/01/13
 */
@ControllerAdvice
public class GlobalExceptionHandler {

	private final Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * 处理rest接口异常，返回包装类UOPResponse
	 */
	@ExceptionHandler(value = UOPRestApiException.class)
	@ResponseBody
	public UOPRestApiResponse onRestException(UOPRestApiException e) {
		logger.error(e.toString());
		e.printStackTrace();

		UOPRestApiResponse rsp = new UOPRestApiResponse();
		rsp.getMeta().put("code", UOPRestApiResponse.FAILED);
		rsp.setError(Collections.singletonMap("message", e.getMessage()));
		return rsp;
	}
}
